#include "choosevoicedialog.h"
#include "ui_choosevoicedialog.h"

#include <QCheckBox>
#include <QGridLayout>
#include <QMessageBox>

ChooseVoiceDialog::ChooseVoiceDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ChooseVoiceDialog)
{
    ui->setupUi(this);
    QStringList casts;
    casts << "声优1"
         << "声优2"
         << "声优3"
         << "声优4"
         << "声优5"
         << "声优6"
         << "声优7"
         << "声优8"
         << "声优9";

    int count = casts.count();
    QGridLayout *gridlayout = new QGridLayout(ui->widget);

    int index = 0;
    for(auto p : casts)
    {
        QCheckBox *checkbox = new QCheckBox(p);
        m_boxes.append(checkbox);

        int row = index / 4;
        int col = index % 4;
        index++;

        gridlayout->addWidget(checkbox, row, col);

        connect(checkbox, &QCheckBox::toggled, this, [=](bool toggled){
            if(toggled)
            {
                m_casts.append(checkbox->text());
            }
            else
            {
                m_casts.removeOne(checkbox->text());
            }
        });
    }
}

ChooseVoiceDialog::~ChooseVoiceDialog()
{
    delete ui;
}

void ChooseVoiceDialog::setCasts(QStringList casts)
{
    for(auto p : m_boxes)
    {
        p->setChecked(casts.contains(p->text()));
    }
}

QStringList ChooseVoiceDialog::getCasts()
{
    return m_casts;
}

void ChooseVoiceDialog::on_btnOk_clicked()
{
    if(m_casts.isEmpty())
    {
        QMessageBox::warning(this, "警告", "请至少选择一个标签");
        return;
    }

    accept();
}

void ChooseVoiceDialog::on_btnCancel_clicked()
{
        reject();
}
